package cn.yunyichina.openapi.sdk.utils;

import cn.yunyichina.openapi.sdk.security.*;

import java.util.HashMap;
import java.util.Map;

import static cn.yunyichina.openapi.sdk.config.OpenApiConfig.getConfig;

/**
 * api工具类
 *
 * @author zhangkewen
 */
public class ApiUtil {

    public static String httpPost(String sessionId, String method, String data) {
        String result;

        try {
            String opeanApiUrl = getConfig().getOpenApiUrl();

            String appid = getConfig().getAppId();
            String format = getConfig().getFormat();
            String v = getConfig().getV();
            String signKey = getConfig().getSignKey();
//            String yunyichinaPublicKey = OpenApiConfig.getConfig().getYunyichinaPublicKey();
//            String merchantPublicKey = OpenApiConfig.getConfig().getMerchantPublicKey();
//            String merchantPrivateKey = OpenApiConfig.getConfig().getMerchantPrivateKey();
            String timestamp = TimeUtil.getTime();
            String postData = Base64Method.encryptUrlSafeBase64(data);
            int signType = getConfig().getSignType().intValue();

            Map<String, String> maps = new HashMap<>();
            maps.put("app_id", appid);
            maps.put("format", format);
            maps.put("v", v);
            maps.put("timestamp", timestamp);
            maps.put("data", postData);
            maps.put("method", method);
            maps.put("sign_type", String.valueOf(signType));
            if (sessionId != null && !"".equals(sessionId.trim())) {
                maps.put("sessionid", sessionId);
            }

            String encryptStr = MapUtil.coverMap2SignString(maps);
            String sign = "";

            if (signType == 1) {
                sign = MD5Util.string2MD5(AesEncryption.encrypt(encryptStr, signKey, signKey));
            } else if (signType == 2) {
                sign = MD5Util.string2MD5("helloworld");
            } else if (signType == 3) {
                sign = MD5Util.string2MD5(encryptStr + signKey);
            }

            maps.put("sign", sign);

            String httpParams = MapUtil.coverMap2String(maps);
            if (opeanApiUrl.startsWith("https")) {
                result = HttpsUtil.doSslPost(opeanApiUrl, httpParams, "utf-8");
            } else {
                result = HttpsUtil.doPost(opeanApiUrl, httpParams);
            }

            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            result = ex.getMessage();
        }

        return result;
    }

    public static String httpPostLogin(String method, String data) {
        String result;

        try {
            String opeanApiUrl = getConfig().getOpenApiUrl();

            String appid = getConfig().getAppId();
            String format = getConfig().getFormat();
            String v = getConfig().getV();
            String timestamp = TimeUtil.getTime();
            String postData = Base64Method.encryptUrlSafeBase64(data);

            Map<String, String> maps = new HashMap<>();
            maps.put("app_id", appid);
            maps.put("format", format);
            maps.put("v", v);
            maps.put("timestamp", timestamp);
            maps.put("data", postData);
            maps.put("method", method);

            String httpParams = MapUtil.coverMap2String(maps);
            result = HttpsUtil.doSslPost(opeanApiUrl, httpParams, "utf-8");
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            result = ex.getMessage();
        }

        return result;
    }

}
